<?php
/**
 * Created by PhpStorm.
 * User: zhengze
 * Date: 2019/7/1
 * Time: 18:29
 */

namespace app\admin\logic;

use app\common\exception\ExceptionEmun;
use app\common\exception\UserException;
use Helper\ArrayHelper;
use app\common\logic\BaseLogic;
use app\common\model\Member;
use app\common\model\MemberGroupRelation;
use app\common\traits\InstanceTrait;
use app\member\validate\MemberValidate;
use think\exception\DbException;
use think\facade\Db;

/**
 * Class MemberLogic
 * @package app\admin\logic
 */
class MemberLogic extends BaseLogic
{
    use InstanceTrait;

    public function __construct($model = NULL)
    {
        $model = !empty($model) ? $model : Member::instance();
        parent::__construct($model);
    }
    /**
     * @param $param
     */
    public function read($member_id)
    {
        $Member = Member::instance();
        $member = $Member->getByID($member_id);
        if (empty($member)) {
            ExceptionEmun::throwException(UserException::NO_EXIST);
        }
        $member->hidden(['password']);
        return $member;
    }

    /**
     * 创建用户
     * @param array $param [description]
     */
    public function createData($param)
    {
        // 验证
        Member::instance()->startTrans();
        $param['phone'] = 0;
        $param['password'] = encrypt_password($param['password']);
        $Member = Member::create($param);
//        太慢,废除
//        $Member->groups()->saveAll($param['groups']);
        $this->insertGroupRelation($Member['member_id'], $param['groups']);
        Member::instance()->commit();
        return true;
    }


    /**
     * 通过id修改用户
     * @param array $param [description]
     */
    public function updateByID($param)
    {
        $Member = Member::instance();
        $id = $param[$Member->getPk()] ?? $param['id'];
        $Member->startTrans();

        $member = Member::field($Member->baseField)->where('member_id', '=', $id)->find();
        if (!$member) {
            ExceptionEmun::throwException(UserException::NO_EXIST);
        }
        $member->groups()->detach();
//        太慢,废除
//        $member->groups()->saveAll($param['groups']);
        $this->insertGroupRelation($id, $param['groups']);
        //填了密码就改密码,不填就不改密码
        if (!empty($param['password'])) {
            $param['password'] = encrypt_password($param['password']);
        } else {
            unset($param['password']);
        }
        $member->save($param);
        $Member->commit();
        return true;

    }

    /**
     * 获取列表
     * @param $param
     * @return mixed|\think\Model|\think\model\Pivot|null
     */
    public function getList($param)
    {
        $data = Member::instance()->getList($param);
        return $data->hidden(['password']);
    }

    /**
     * 获取统计
     * @param $param
     * @return mixed|\think\Model|\think\model\Pivot|null
     */
    public function getCount($param)
    {
        $data = Member::instance()->getCount($param);
        return $data;
    }

    /**
     * AdminGroupRelation中间表写入
     * @param $group_id
     * @param $member_id
     * @param $menus
     */
    protected function insertGroupRelation($member_id, $groups)
    {

        $groups = ArrayHelper::ToTree($groups);
        $data = MemberGroupRelation::beforeInsertData([
            'member_id' => $member_id
        ]);
        $insertAllDate = [];
        foreach ($groups as $key => $value) {
            $insertAllDate[$key] = $data;
            $insertAllDate[$key]['group_id'] = $value;
        }
        Db::name(MemberGroupRelation::instance()->getTable())->insertAll($insertAllDate);
    }
}
